import 'package:boss/models/package_group.dart';
import 'package:boss/ui/page/commodity/categories/page/plusCommodityMode/page/plusPackage/provider/package_detail_provider.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/sortList/sort_item_data.dart';
import 'package:boss/ui/widget/sortList/sort_util.dart';
import 'package:hi_common/hi_common.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

import 'must_commodity_header.dart';
import 'must_commodity_item.dart';

class MustCommodityList extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    var provider = Provider.of<PackageDetailProvider>(context);
    var mustCommodityList = provider.package.mustCommodityList;

    if (mustCommodityList.length == 0) {
      return SizedBox();
    }

    List<Widget> children = [];

    children.add(MustCommodityHeader());

    mustCommodityList.forEach((item) {
      children.add(MustCommodityItem(
        data: item,
        key: item.key,
      ));
    });

    children.add(Container(
      height: 80.w,
      alignment: Alignment.centerRight,
      child: LabelButton(
        title: '调整排序',
        onTap: () async {
          var res = await showSortList<CommodityFormList>(
            context,
            title: '菜品排序',
            items: mustCommodityList
                .map(
                  (item) => SortItemData(title: '${item.commodityNameWithSku}        ${AmountUtil.price2String(item.price)}元', value: item),
                )
                .toList(),
          );
          if (res != null) {
            provider.sortMustCommodity(res);
          }
        },
      ),
    ));

    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: children,
    );
  }
}
